package com.jinsi.controller.ylt;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.jinsi.pojo.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * @ClassName FastController
 * @Content 上传文件服务器
 * @Author 姚龙腾
 * @Date 2022/10/12 10:40
 */
@Controller
@Slf4j
public class FastController {

    @Autowired
    private FastFileStorageClient client;

    @ResponseBody
    @RequestMapping(value = "/YltUpload",method = RequestMethod.POST)
    public JsonResult upload(@RequestParam("file") MultipartFile file) {
        log.info("上传文件服务器开始执行 文件名${}" ,file.getName());
        JsonResult res = new JsonResult();
        try {
            InputStream inputStream = file.getInputStream();

            String fileName = file.getOriginalFilename();
            String extName = fileName.substring(fileName.lastIndexOf(".")+1);
            //上传
            StorePath storePath = client.uploadFile(inputStream,file.getSize(),extName , null);

            log.info("上传文件服务器执行成功 文件名${}" ,file.getName());
            System.out.println(storePath.getGroup());
            System.out.println(storePath.getPath());
            System.out.println(storePath.getFullPath());

            res.setCode(0);
            res.setData(storePath);
        } catch (Exception e) {
            log.error("上传文件服务器执行失败 报错原因 ${}" ,e);
            res.setCode(1);
        }
        return res;
    }

}
